package com.yscz.peexcel.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yscz.peexcel.common.Result;
import com.yscz.peexcel.config.YmlValueConfig;
import com.yscz.peexcel.entity.*;
import com.yscz.peexcel.mapper.*;
import com.yscz.peexcel.service.OptionService;
import com.yscz.peexcel.service.PoiExcelService;
import com.yscz.peexcel.util.PoiExcelUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;

/**
 * @author achen
 * @create 2021-12-02 10:47
 */
@Service
@Transactional
@Slf4j
public class PoiExcelServiceImpl implements PoiExcelService {
    @Autowired
    private YmlValueConfig ymlValueConfig;

    @Autowired
    private ScaleMapper scaleMapper;

    @Autowired
    private AlgMapper algMapper;

    @Autowired
    private ScaleAlgMapper scaleAlgMapper;

    @Autowired
    private ScaleProjectMapper scaleProjectMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private OptionMapper optionMapper;

    @Autowired
    private FactorMapper factorMapper;

    @Autowired
    private QuestionFactorMapper questionFactorMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    @SneakyThrows
    @Override
    public void readScaleExcel() {
        String location = YmlValueConfig.getLocation();
        //获取文件夹底下所有的文件
        List<String> fileNames = readFileNames();
        for (String fileName : fileNames) {
            String fileLocation = location + "/" + fileName;

            //从第一个Sheet中量表内容
            //量表名
            String scaleName = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 0, 1);
            //量表分类ID
            String scaleTypeId = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 0, 2);
            double doubleScaleTypeId = Double.parseDouble(scaleTypeId);
            Long longScaleTypeId = null;
            if (doubleScaleTypeId < 10) {
                longScaleTypeId = Long.parseLong(scaleTypeId.substring(0, 1));
            } else if (doubleScaleTypeId >= 10) {
                longScaleTypeId = Long.parseLong(scaleTypeId.substring(0, 2));
            }
            //量表编撰人
            String scaleUserName = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 0, 3);
            //量表内容
            String scaleContent = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 1, 1);
            //量表项目对应关系
            String[] stringScaleProjectIds = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 1, 2).split(",");
            Long[] scaleProjectIds = Convert.toLongArray(stringScaleProjectIds);

            //题目数量
            Integer questionNum = Integer.parseInt(PoiExcelUtils.readExcelData(fileLocation, "量表详情", 2, 1).substring(0, 2));

            String totalScaleAlg = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 3, 1);
            String factorScaleAlg = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 4, 1);
            String posAndNegScaleAlg = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 5, 1);
            String sunAndShadeScaleAlg = PoiExcelUtils.readExcelData(fileLocation, "量表详情", 6, 1);

            //第一个Sheet
            //添加量表
            Scale scale = addScale(scaleName, scaleContent, questionNum, longScaleTypeId,scaleUserName);
            log.info("scale:[{}]", scale);

            //添加量表算法表（量表下总分、因子分、正反、阴阳对应的不同的计算规则）
            addScaleAlg(scale.getScaleId(), totalScaleAlg, factorScaleAlg, posAndNegScaleAlg, sunAndShadeScaleAlg);

            //添加量表行业表
            addScaleProject(scaleProjectIds, scale.getScaleId());

            //第二个Sheet
            //读取第二个Sheet的内容
            ImportParams params2 = new ImportParams();
            //设置表头的行数
            params2.setHeadRows(1);
            params2.setStartSheetIndex(1);
            List<ScaleExcel> scaleExcels = ExcelImportUtil.importExcel(new File(fileLocation), ScaleExcel.class, params2);

            //第三个Sheet
            //读取第三个Sheet的内容
            ImportParams params3 = new ImportParams();
            //设置表头的行数
            params3.setHeadRows(1);
            params3.setStartSheetIndex(2);
            List<ScoreExcel> scoreExcels = ExcelImportUtil.importExcel(new File(fileLocation), ScoreExcel.class, params3);

            //添加题目表、选项表信息、因子表、题目因子表
            addQuestion(scaleExcels, scale.getScaleId(), totalScaleAlg, factorScaleAlg, posAndNegScaleAlg, sunAndShadeScaleAlg, scoreExcels);
        }
    }

    /**
     * 添加量表
     *
     * @param scaleName    量表名
     * @param scaleContent 量表内容
     * @param questionNum  题目数量
     * @param scaleTypeId  量表分类ID
     * @return scale
     */
    private Scale addScale(String scaleName, String scaleContent, Integer questionNum, Long scaleTypeId,String scaleUserName) {
        String scaleMaxSort = getScaleMaxSort();
        Scale scale = new Scale().setScaleName(scaleName).setScaleContent(scaleContent).setQuestionNum(questionNum).setUpdateTime(new Date()).setScaleTypeIdFk(scaleTypeId).setScaleSort(scaleMaxSort).setEditor(scaleUserName);
        scaleMapper.insert(scale);

        return scale;
    }

    /**
     * 添加量表算法规则
     *
     * @param scaleId             量表ID
     * @param totalScaleAlg       总分算法规则
     * @param factorScaleAlg      因子分算法规则
     * @param posAndNegScaleAlg   正反算法规则
     * @param sunAndShadeScaleAlg 阴阳算法规则
     */
    private void addScaleAlg(Long scaleId, String totalScaleAlg, String factorScaleAlg, String posAndNegScaleAlg, String sunAndShadeScaleAlg) {
        //默认的计算规则是求总分的
        //总分规则
        if (!"".equals(totalScaleAlg)) {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "总分");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("总分").setOperator(totalScaleAlg).setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        } else { //默认是总分的计算规则
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "总分");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("总分").setOperator("总分").setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        }

        //因子分规则
        if (!"".equals(factorScaleAlg)) {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "因子分");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("因子分").setOperator(factorScaleAlg).setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        } else {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "因子分");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("因子分").setOperator("总分").setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        }

        //正反规则
        if (!"".equals(posAndNegScaleAlg)) {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "正反");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("正反").setOperator(posAndNegScaleAlg).setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        } else {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "正反");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("正反").setOperator("总分").setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        }

        //阴阳规则
        if (!"".equals(sunAndShadeScaleAlg)) {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "阴阳");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("阴阳").setOperator(sunAndShadeScaleAlg).setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        } else {
            QueryWrapper<Alg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("alg_name", "阴阳");
            Alg alg = algMapper.selectOne(queryWrapper);
            ScaleAlg scaleAlg = new ScaleAlg().setFactorName("阴阳").setOperator("总分").setScaleIdFk(scaleId).setAlgIdFk(alg.getAlgId());
            scaleAlgMapper.insert(scaleAlg);
        }
    }

    /**
     * 添加量表行业关联表
     *
     * @param scaleProjectIds 行业ID
     * @param scaleId         量表ID
     */
    private void addScaleProject(Long[] scaleProjectIds, Long scaleId) {
        if (null != scaleProjectIds) {
            ScaleProject scaleProject = new ScaleProject();
            for (long scaleProjectId : scaleProjectIds) {
                scaleProject.setScaleId(scaleId).setProjectId(scaleProjectId);
                scaleProjectMapper.insert(scaleProject);
            }
        }
    }

    /**
     * 添加题目表
     *
     * @param scaleExcels 第二个Sheet的内容
     */
    private void addQuestion(List<ScaleExcel> scaleExcels, Long scaleId, String totalScaleAlg, String factorScaleAlg, String posAndNegScaleAlg, String sunAndShadeScaleAlg, List<ScoreExcel> scoreExcels) {
        //添加因子表
        addFactor(scaleExcels);

        //添加阈值规则
        addScore(scoreExcels, scaleId);

        Iterator<ScaleExcel> scaleExcelIterator = scaleExcels.iterator();
        Question question = new Question();
        while (scaleExcelIterator.hasNext()) {
            ScaleExcel scaleExcel = scaleExcelIterator.next();

            //添加试题信息
            question.setQuestionNum(scaleExcel.getId()).setQuestionName(scaleExcel.getQuestionName()).setScaleIdFk(scaleId);
            questionMapper.insert(question);

            //添加试题因子表
            addQuestionFactor(scaleExcel, question, totalScaleAlg, factorScaleAlg, posAndNegScaleAlg, sunAndShadeScaleAlg);

            //添加选项信息
            addOption(scaleExcel, question);
        }
    }

    /**
     * 添加选项信息
     *
     * @param scaleExcel 量表题目excel信息
     * @param question   题目信息
     */
    private void addOption(ScaleExcel scaleExcel, Question question) {
        //选项A
        if (null != scaleExcel.getQuestionAContent()) {
            Option option = new Option();
            option.setOptionType("A");
            option.setOptionContent(scaleExcel.getQuestionAContent());
            option.setOptionScore(scaleExcel.getQuestionAScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);
        }
        //选项B
        if (null != scaleExcel.getQuestionBContent()) {
            Option option = new Option();
            option.setOptionType("B");
            option.setOptionContent(scaleExcel.getQuestionBContent());
            option.setOptionScore(scaleExcel.getQuestionBScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
        //选项C
        if (null != scaleExcel.getQuestionCContent()) {
            Option option = new Option();
            option.setOptionType("C");
            option.setOptionContent(scaleExcel.getQuestionCContent());
            option.setOptionScore(scaleExcel.getQuestionCScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
        //选项D
        if (null != scaleExcel.getQuestionDContent()) {
            Option option = new Option();
            option.setOptionType("D");
            option.setOptionContent(scaleExcel.getQuestionDContent());
            option.setOptionScore(scaleExcel.getQuestionDScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
        //选项E
        if (null != scaleExcel.getQuestionEContent()) {
            Option option = new Option();
            option.setOptionType("E");
            option.setOptionContent(scaleExcel.getQuestionEContent());
            option.setOptionScore(scaleExcel.getQuestionEScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
        //选项F
        if (null != scaleExcel.getQuestionFContent()) {
            Option option = new Option();
            option.setOptionType("F");
            option.setOptionContent(scaleExcel.getQuestionFContent());
            option.setOptionScore(scaleExcel.getQuestionFScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
        //选项G
        if (null != scaleExcel.getQuestionGContent()) {
            Option option = new Option();
            option.setOptionType("G");
            option.setOptionContent(scaleExcel.getQuestionGContent());
            option.setOptionScore(scaleExcel.getQuestionGScore());
            option.setQuestionIdFk(question.getQuestionId());
            optionMapper.insert(option);

        }
    }


    /**
     * 添加因子信息
     *
     * @param scaleExcels 量表题目excel信息
     */
    private void addFactor(List<ScaleExcel> scaleExcels) {
        //添加因子表
        Iterator<ScaleExcel> factorIterator = scaleExcels.iterator();
        Factor factor = new Factor();
        //查询因子分的algId
        QueryWrapper<Alg> algWrapper = new QueryWrapper<>();
        algWrapper.eq("alg_name", "因子分");
        Alg alg = algMapper.selectOne(algWrapper);
        while (factorIterator.hasNext()) {
            ScaleExcel scaleExcel = factorIterator.next();
            //查询因子表中是否存在该因子
            QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
            factorWrapper.eq("factor_name", scaleExcel.getFactorName());
            Factor factorDB = factorMapper.selectOne(factorWrapper);
            //不存在该因子则添加
            if (null != scaleExcel.getFactorName() && null == factorDB) {
                //添加到因子表 返回因子id
                factor.setFactorName(scaleExcel.getFactorName());
                factor.setCreateTime(new Date());
                factor.setUpdateTime(new Date());
                factor.setAlgIdFk(alg.getAlgId());
                //添加因子
                factorMapper.insert(factor);
            }
        }

        //默认添加总分、正、反、阴、阳因子
        addOtherFactor();
    }

    /**
     * 默认添加总分、正、反、阴、阳因子
     */
    private void addOtherFactor() {
        Factor totalFactor = new Factor();
        //查询总分因子分的algId
        QueryWrapper<Alg> totalAlgWrapper = new QueryWrapper<>();
        totalAlgWrapper.eq("alg_name", "总分");
        Alg totalAlg = algMapper.selectOne(totalAlgWrapper);
        //查询因子表中是否存在该因子
        QueryWrapper<Factor> totalFactorWrapper = new QueryWrapper<>();
        totalFactorWrapper.eq("factor_name", "总分");
        Factor totalFactorDB = factorMapper.selectOne(totalFactorWrapper);
        //如果不存在则添加
        if (null == totalFactorDB) {
            totalFactor.setFactorName("总分");
            totalFactor.setCreateTime(new Date());
            totalFactor.setUpdateTime(new Date());
            totalFactor.setAlgIdFk(totalAlg.getAlgId());
            factorMapper.insert(totalFactor);
        }

        //查询正反因子分的algId
        Factor posFactor = new Factor();
        Factor negFactor = new Factor();
        QueryWrapper<Alg> posAndNegAlgWrapper = new QueryWrapper<>();
        posAndNegAlgWrapper.eq("alg_name", "正反");
        Alg posAndNegAlg = algMapper.selectOne(posAndNegAlgWrapper);
        //查询因子表中是否存在正因子
        QueryWrapper<Factor> posFactorWrapper = new QueryWrapper<>();
        posFactorWrapper.eq("factor_name", "正");
        Factor posFactorDB = factorMapper.selectOne(posFactorWrapper);
        //如果不存在则添加
        if (null == posFactorDB) {
            posFactor.setFactorName("正");
            posFactor.setCreateTime(new Date());
            posFactor.setUpdateTime(new Date());
            posFactor.setAlgIdFk(posAndNegAlg.getAlgId());
            factorMapper.insert(posFactor);
        }

        //查询因子表中是否存在反因子
        QueryWrapper<Factor> negFactorWrapper = new QueryWrapper<>();
        negFactorWrapper.eq("factor_name", "反");
        Factor negFactorDB = factorMapper.selectOne(negFactorWrapper);
        //如果不存在则添加
        if (null == negFactorDB) {
            negFactor.setFactorName("反");
            negFactor.setCreateTime(new Date());
            negFactor.setUpdateTime(new Date());
            negFactor.setAlgIdFk(posAndNegAlg.getAlgId());
            factorMapper.insert(negFactor);
        }


        //查询阴阳因子分的algId
        Factor sunFactor = new Factor();
        Factor shadeFactor = new Factor();
        QueryWrapper<Alg> sunAndShadeAlgWrapper = new QueryWrapper<>();
        sunAndShadeAlgWrapper.eq("alg_name", "阴阳");
        Alg sunAndShadeAlg = algMapper.selectOne(sunAndShadeAlgWrapper);

        //查询因子表中是否存在阳因子
        QueryWrapper<Factor> sunFactorWrapper = new QueryWrapper<>();
        sunFactorWrapper.eq("factor_name", "阳");
        Factor sunFactorDB = factorMapper.selectOne(sunFactorWrapper);
        //如果不存在则添加
        if (null == sunFactorDB) {
            sunFactor.setFactorName("阳");
            sunFactor.setCreateTime(new Date());
            sunFactor.setUpdateTime(new Date());
            sunFactor.setAlgIdFk(sunAndShadeAlg.getAlgId());
            factorMapper.insert(sunFactor);
        }

        //查询因子表中是否存在阳因子
        QueryWrapper<Factor> shadeFactorWrapper = new QueryWrapper<>();
        shadeFactorWrapper.eq("factor_name", "阴");
        Factor shadeFactorDB = factorMapper.selectOne(shadeFactorWrapper);
        //如果不存在则添加
        if (null == shadeFactorDB) {
            shadeFactor.setFactorName("阴");
            shadeFactor.setCreateTime(new Date());
            shadeFactor.setUpdateTime(new Date());
            shadeFactor.setAlgIdFk(sunAndShadeAlg.getAlgId());
            factorMapper.insert(shadeFactor);
        }
    }

    /**
     * 添加题目因子关联信息
     *
     * @param scaleExcel          量表题目excel信息
     * @param question            问题信息
     * @param totalScaleAlg       总分算法规则
     * @param factorScaleAlg      因子分算法规则
     * @param posAndNegScaleAlg   正反分算法规则
     * @param sunAndShadeScaleAlg 阴阳分算法规则
     */
    private void addQuestionFactor(ScaleExcel scaleExcel, Question question, String totalScaleAlg, String factorScaleAlg, String posAndNegScaleAlg, String sunAndShadeScaleAlg) {
        //判断是否有因子算法
        if (null != factorScaleAlg && null != scaleExcel.getFactorName()) {
            QuestionFactor questionFactor = new QuestionFactor();
            //查询是否有该因子
            QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
            factorWrapper.eq("factor_name", scaleExcel.getFactorName());
            Factor factor = factorMapper.selectOne(factorWrapper);
            if (null != factor) {
                questionFactor.setFactorId(factor.getFactorId());
                questionFactor.setQuestionId(question.getQuestionId());
                questionFactorMapper.insert(questionFactor);
            }
        }

        //判断是否有正反分算法
        if (null != posAndNegScaleAlg && null != scaleExcel.getPositiveAndNegative()) {
            QuestionFactor questionFactor = new QuestionFactor();
            //查询是否有该因子
            QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
            if (1 == scaleExcel.getPositiveAndNegative()) {
                factorWrapper.eq("factor_name", "正");
            } else if (0 == scaleExcel.getPositiveAndNegative()) {
                factorWrapper.eq("factor_name", "反");
            }
            Factor factor = factorMapper.selectOne(factorWrapper);
            if (null != factor) {
                questionFactor.setFactorId(factor.getFactorId());
                questionFactor.setQuestionId(question.getQuestionId());
                questionFactorMapper.insert(questionFactor);
            }
        }
    }

    /**
     * 添加阈值规则信息
     *
     * @param scoreExcels 阈值excel信息
     */
    private void addScore(List<ScoreExcel> scoreExcels, Long scaleId) {
        Iterator<ScoreExcel> scoreExcelIterator = scoreExcels.iterator();
        while (scoreExcelIterator.hasNext()) {
            ScoreExcel scoreExcel = scoreExcelIterator.next();
            //添加总分阈值规则
            if ("总分".equals(scoreExcel.getType())) {
                //查询总分因子id
                QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                factorWrapper.eq("factor_name", scoreExcel.getType());
                Factor factor = factorMapper.selectOne(factorWrapper);
                if (null != factor) {
                    Score score = new Score().setAlgType("总分").setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                    scoreMapper.insert(score);
                }
            }

            //添加因子分阈值规则
            if ("因子分".equals(scoreExcel.getType())) {
                //查询总分因子id
                if (null != scoreExcel.getFactorName()) {
                    QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                    factorWrapper.eq("factor_name", scoreExcel.getFactorName());
                    Factor factor = factorMapper.selectOne(factorWrapper);
                    if (null != factor) {
                        Score score = new Score().setAlgType(scoreExcel.getFactorName()).setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                        scoreMapper.insert(score);
                    }
                }
            }

            //添加正反分阈值规则
            if ("正".equals(scoreExcel.getType())) {
                //查询总分因子id
                QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                factorWrapper.eq("factor_name", scoreExcel.getType());
                Factor factor = factorMapper.selectOne(factorWrapper);
                if (null != factor) {
                    Score score = new Score().setAlgType("正").setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                    scoreMapper.insert(score);
                }
            }

            if ("反".equals(scoreExcel.getType())) {
                //查询总分因子id
                QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                factorWrapper.eq("factor_name", scoreExcel.getType());
                Factor factor = factorMapper.selectOne(factorWrapper);
                if (null != factor) {
                    Score score = new Score().setAlgType("反").setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                    scoreMapper.insert(score);
                }
            }

            //添加阴阳分阈值规则
            if ("阴".equals(scoreExcel.getType())) {
                //查询总分因子id
                QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                factorWrapper.eq("factor_name", scoreExcel.getType());
                Factor factor = factorMapper.selectOne(factorWrapper);
                if (null != factor) {
                    Score score = new Score().setAlgType("阴").setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                    scoreMapper.insert(score);
                }
            }

            if ("阳".equals(scoreExcel.getType())) {
                //查询总分因子id
                QueryWrapper<Factor> factorWrapper = new QueryWrapper<>();
                factorWrapper.eq("factor_name", scoreExcel.getType());
                Factor factor = factorMapper.selectOne(factorWrapper);
                if (null != factor) {
                    Score score = new Score().setAlgType("阳").setScoreMin(scoreExcel.getScoreMin()).setScoreMax(scoreExcel.getScoreMax()).setScoreContent(scoreExcel.getScoreContent()).setFactorIdFk(factor.getFactorId()).setScaleIdFk(scaleId);
                    scoreMapper.insert(score);
                }
            }
        }
    }

    /**
     * 获取文件夹下的所有文件
     */
    private List<String> readFileNames() {
        String filePath = YmlValueConfig.getLocation();
        ;
        File file = new File(filePath);
        List<String> fileNames = new ArrayList<>();
        if (file.isDirectory()) {
            log.info("文件夹");
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(filePath + "\\" + filelist[i]);
                if (!readfile.isDirectory()) {
                    log.info("path:[{}]", readfile.getPath());
                    log.info("absolutePath:[{}]", readfile.getAbsolutePath());
                    log.info("name:[{}]", readfile.getName());
                    fileNames.add(readfile.getName());
                }
            }
        }
        return fileNames;
    }

    /**
     * 获取量表最大的排序号
     */
    private String getScaleMaxSort() {
        //获取最大的sort值
        List<Scale> scales = scaleMapper.selectList(null);
        Optional<Scale> max = scales.stream().max((scale1, scale2) -> Integer.compare(Integer.parseInt(scale1.getScaleSort()), Integer.parseInt(scale2.getScaleSort())));
        System.out.println(max);
        String scaleSort = null;
        if (!max.equals(Optional.empty())) {
            scaleSort = max.get().getScaleSort();
            return String.valueOf(Integer.parseInt(scaleSort) + 1);
        } else {
            scaleSort = "1";
            return scaleSort;
        }
    }
}
